import Vue from 'vue'
import App from './App.vue'
import router from './router'
import store from './store';
import ElementUI from 'element-ui';
import 'element-ui/lib/theme-chalk/index.css';
import {getRequest} from './utils/api';
import {postRequest} from './utils/api';
import {putRequest} from './utils/api';
import {deleteRequest} from './utils/api';
import {initMenu} from "./utils/menu";

Vue.config.productionTip = false
Vue.use(ElementUI);
Vue.prototype.getRequest = getRequest;
Vue.prototype.postRequest = postRequest;
Vue.prototype.putRequest = putRequest;
Vue.prototype.deleteRequest = deleteRequest;
import 'font-awesome/css/font-awesome.min.css'

/**
 * router.beforeEach方法里有三个参数，一个是to到哪去，一个是from从哪来，一个是next方法进行下一步
 * 判断要去的路径是不是/，如果是就执行下一步；
 * 如果不是就看是否能获取用户数据，获取到，就渲染菜单数据并执行下一步；
 * 否则重定向到登录页
 */
router.beforeEach((to, from, next) => {
    if (to.path == '/') {
        next();
    } else {
        if (window.sessionStorage.getItem("user")) {
            initMenu(router, store);
            next();
        } else {
            //重定向到要去的页面
            next('/?redirect=' + to.path);
        }
    }
})

new Vue({
    router,
    store,
    render: h => h(App)
}).$mount('#app')
